//============================================================================================
/**
 * @file	scr_sodateya.c
 * @brief	スクリプトコマンド：そだてや関連
 * @author	Tomomichi Ohta
 * @date	06.06.26
 */
//============================================================================================
#include "common.h"
#include "system/lib_pack.h"
#include "fieldsys.h"

#include "script.h"
#include "scrcmd_def.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "sysflag.h"
#include "syswork.h"

#include "battle/battle_server.h"
#include "itemtool/itemsym.h"

#include "savedata/sodateyadata.h"
#include "sodateya.h"

#include "scr_sodateya.h"

//============================================================================================
//
//	コマンド
//
//============================================================================================


//============================================================================================
//
//	育て屋関連
//
//============================================================================================

//--------------------------------------------------------------------------------------------
/**
 * そだてや　名前の取得
 *
 * @param	core		仮想マシン制御構造体へのポインタ
 *
 * @return	"0"
 */
//--------------------------------------------------------------------------------------------
BOOL EvCmdGetSodateyaName( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	WORDSET** wordset	= GetEvScriptWorkMemberAdrs( fsys, ID_EVSCR_WORDSET );

	SodateyaNameSet(SaveData_GetSodateyaWork(sv),*wordset);

	return 0;
}

//--------------------------------------------------------------------------------------------
/**
 * そだてや　じいさんの状態取得
 *
 * @param	core		仮想マシン制御構造体へのポインタ
 *
 * @return	"0"
 */
//--------------------------------------------------------------------------------------------
BOOL EvCmdGetSodateyaZiisan( VM_MACHINE * core )
{
	u16 * work;
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	SODATEYA_WORK *sodateya;
	work = VMGetWork( core );

	sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));
	*work = SodateyaZiisannCheck(sodateya);

	return 0;
}

//--------------------------------------------------------------------------------------------
/**
 *			そだてやにいるタマゴを消去
 * @brief   そだてや用のワークから、タマゴがいる情報をクリアする
 *
 * @param	core		仮想マシン制御構造体へのポインタ
 *
 * @return	"0"
 */
//--------------------------------------------------------------------------------------------
BOOL EvCmdDelSodateyaEgg( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	SODATEYA_WORK *sodateya;
	sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));

	DelSodateyaTamagoSub(sodateya);

	return 0;
}


BOOL EvCmdGetSodateyaEgg( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	SODATEYA_WORK *sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));
	POKEPARTY     *temoti   = SaveData_GetTemotiPokemon(fsys->savedata);
	MYSTATUS* my	= SaveData_GetMyStatus( GameSystem_GetSaveData(core->fsys) );

	PokeTamagoDel(sodateya, temoti, my );

//作成中 060302 by tomo
//xxxxx

	return 0;
}


BOOL EvCmdHikitoriPoke( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SODATEYA_WORK *sodateya;
	POKEPARTY * temoti;
	WORDSET** wordset		= GetEvScriptWorkMemberAdrs( fsys, ID_EVSCR_WORDSET );
	SAVEDATA * sv = fsys->savedata;
	u16 *ret_wk		= VMGetWork(core);
	u8 no		= VMGetWorkValue(core);
	sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));
	temoti = SaveData_GetTemotiPokemon(fsys->savedata);

	*ret_wk = PokeHikitoriSub(temoti,*wordset,sodateya,no);

	return 0;
}

// =================================================================
/**
 * @brief 引き取り料金取得
 * @param  none
 * @retval  料金を返す（引数として、ワークに番号をセットする必要があります）
 * @retval  文字バッファ０へ料金セット、料金をワークへ返す
 */
// =================================================================
BOOL EvCmdHikitoriRyoukin( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SODATEYA_WORK *sodateya;
	WORDSET** wordset		= GetEvScriptWorkMemberAdrs( fsys, ID_EVSCR_WORDSET );
	SAVEDATA * sv = fsys->savedata;
	u16 * work  = VMGetWork(core);
	u8 no		= VMGetWorkValue(core);
	sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));

	*work = PokeHikitoriRyoukinSub(sodateya,no,*wordset);
	return 0;
}

BOOL EvCmdSodatePokeLevelStr( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	SODATEYA_WORK *sodateya;
	WORDSET** wordset	= GetEvScriptWorkMemberAdrs( core->fsys, ID_EVSCR_WORDSET );
	u16 *ret_wk		= VMGetWork( core );
	u16 no			= VMGetWorkValue( core );

	sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));
	*ret_wk = SodatePokeLevelSet(sodateya,no,*wordset );

	return 0;
}

BOOL EvCmdMsgAzukeSet( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	POKEPARTY * temoti;
	WORDSET** wordset	= GetEvScriptWorkMemberAdrs( core->fsys, ID_EVSCR_WORDSET );
	u16* ret_wk			= VMGetWork( core );

	*ret_wk = SodateyaAzukeName(SaveData_GetSodateyaWork(fsys->savedata),*wordset );
	return 0;
}

BOOL EvCmdSetSodateyaPoke( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	SODATEYA_WORK *sodateya;
	POKEPARTY * temoti;
	u8 no				= VMGetWorkValue(core);

	temoti = SaveData_GetTemotiPokemon(fsys->savedata);
	sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));
	PokeSodateSet(temoti,no,sodateya,sv);

	return 0;
}

//--------------------------------------------------------------------------------------------
/**
 * そだてやスクリプト：引き取りLIST用ポケモン名をセット
 *
 * @param	core		仮想マシン制御構造体へのポインタ
 *
 * @return	"0"
 */
//--------------------------------------------------------------------------------------------
BOOL EvCmdHikitoriListNameSet( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	WORDSET** wordset	= GetEvScriptWorkMemberAdrs( core->fsys, ID_EVSCR_WORDSET );

	u8	buf_id		= VMGetWorkValue( core );
	u8	buf2_id		= VMGetWorkValue( core );
	u8	buf3_id		= VMGetWorkValue( core );
	u8	no			= VMGetWorkValue( core );

	SodateyaListNameSet(SaveData_GetSodateyaWork(sv),buf_id,buf2_id,buf3_id,no,*wordset);
	return 0;
}


//--------------------------------------------------------------------------------------------
/**
 * そだてやスクリプト：預けている2体の相性ゲット
 *
 * @param	core		仮想マシン制御構造体へのポインタ
 *
 * @return	"0"
 */
//--------------------------------------------------------------------------------------------
BOOL EvCmdGetSodateyaAishou( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	SODATEYA_WORK *sodateya;
	u16* ret_wk	= VMGetWork( core );

	sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));

	*ret_wk = SodateyaGetAffinity( sodateya );

	return 0;
}

//--------------------------------------------------------------------------------------------
/**
 * そだてやスクリプト：タマゴがうまれているか（１：いる、０：いない）
 *
 * @param	core		仮想マシン制御構造体へのポインタ
 *
 * @return	"0"
 */
//--------------------------------------------------------------------------------------------
BOOL EvCmdGetSodateyaTamagoCheck( VM_MACHINE * core )
{
	FIELDSYS_WORK* fsys = core->fsys;
	SAVEDATA * sv = fsys->savedata;
	SODATEYA_WORK *sodateya;
	u16* ret_wk	= VMGetWork( core );

	sodateya = (SODATEYA_WORK *)(SaveData_Get(sv,GMDATA_ID_SODATEYA));

	*ret_wk = SodateyaWork_GetEggFlag(sodateya);

	OS_Printf("sodateya flag =%08x",*ret_wk);

	return 0;
}

static FIELD_OBJ_PTR AddSodateyaFieldOBJ( CONST_FIELD_OBJ_SYS_PTR fos,u8 id,
		int monsno, u16 formno, int sex, int dir, int x, int z, int zone,const BOOL inRare );

//--------------------------------------------------------------------------------------------
/**
 * 育てやOBJセット
 *
 * @param	core		仮想マシン制御構造体へのポインタ
 *
 * @return	"0"
 */
//--------------------------------------------------------------------------------------------
BOOL EvCmdSodateyaObjSet( VM_MACHINE * core )
{
    FIELD_OBJ_PTR ret;
    FIELDSYS_WORK* fsys = core->fsys;
 //   u16 id				= VMGetWorkValue(core);
	int i;
    FIELD_OBJ_PTR fldobj;		//対象のフィールドOBJのポインタ

	SODATEYA_WORK *sodateya;
	SODATEYA_POKE *s_poke;
	POKEMON_PASO_PARAM* ppp;
	u8	form;
	u16	monsno;

	sodateya = SaveData_GetSodateyaWork(fsys->savedata);

	for(i = 0; i < SODATEYA_POKE_MAX; i++){
		fldobj	= FieldOBJSys_OBJIDSearch( fsys->fldobjsys, FLDOBJ_ID_SODATEYA01+i );

		if( fldobj != NULL ){
			FieldOBJ_DeleteEvent( fldobj );
		}

		s_poke = SodateyaWork_GetSodateyaPokePointer(sodateya, i);
		ppp = SodateyaPoke_GetPokePasoPointer(s_poke);
		if(PokePasoParaGet(ppp,ID_PARA_monsno,NULL) == 0){
			continue;
		}
		form = PokePasoParaGet(ppp, ID_PARA_form_no, NULL);
		monsno = PokePasoParaGet(ppp, ID_PARA_monsno, NULL);
		fldobj = AddSodateyaFieldOBJ( fsys->fldobjsys,i,
			monsno, form,
			PokePasoParaGet(ppp, ID_PARA_sex,NULL),
			DIR_DOWN, 8 + 2*i, 5+4*i, fsys->location->zone_id,
			PokePasoRareGet(ppp));
	}
    return 0;
}

///初期化、ロード時に立てるビット
#define PP_FLDOBJ_INIT_STA_BIT_ON (FLDOBJ_STA_BIT_MOVE_START)
///初期化、ロード時に下ろすビット
#define PP_FLDOBJ_INIT_STA_BIT_OFF (0)

//--------------------------------------------------------------
/**
 * 育てやポケモンフィールドOBJ追加
 * @param	fos			FIELD_OBJ_SYS_PTR
 * @param	obj			OBJコード。HERO等
 * @param	dir			初期方向。DIR_UP等
 * @param	x			グリッドX座標
 * @param	y			グリッドY座標
 * @param	add			追加するか？　TURE:追加		FALSE:ダミーを追加
 * @retval	FIELD_OBJ_PTR
 */
//--------------------------------------------------------------
static FIELD_OBJ_PTR AddSodateyaFieldOBJ( CONST_FIELD_OBJ_SYS_PTR fos,u8 id,
		int monsno, u16 formno, int sex, int dir, int x, int z, int zone,const BOOL inRare )
{
	FIELD_OBJ_PTR fldobj;
	int obj;

	//モンスターナンバーをOBJコードに変換
	obj = PairPoke_GetObjCodeFromMonsNo(monsno, formno, sex);

	fldobj = FieldOBJ_AddHMake( fos, x, z, dir, obj, MV_RND_UDR, zone );
	GF_ASSERT( fldobj != NULL && "player init obj add error" );

	FieldOBJ_OBJIDSet( fldobj, FLDOBJ_ID_SODATEYA01+id );
	FieldOBJ_EventTypeSet( fldobj, 0 );
	FieldOBJ_EventFlagSet( fldobj, 0 );
	FieldOBJ_EventIDSet( fldobj, 0/*SCRID_NULL*/);

	//ワークを初期化（FLDOBJ_PARAM_0,FLDOBJ_PARAM_1は値がセットされるのでクリア省略）
	FieldOBJ_ParamSet( fldobj, 0, FLDOBJ_PARAM_2 );

	//連れ歩きポケモン情報取得
	PairPoke_SetInfo(fldobj, monsno, formno, inRare);

	FieldOBJ_MoveLimitXSet( fldobj, MOVE_LIMIT_NOT );
	FieldOBJ_MoveLimitZSet( fldobj, MOVE_LIMIT_NOT );
	FieldOBJ_StatusBit_ON( fldobj, PP_FLDOBJ_INIT_STA_BIT_ON );
	FieldOBJ_StatusBit_OFF( fldobj, PP_FLDOBJ_INIT_STA_BIT_OFF );
	FieldOBJ_StatusBitSet_HeightExpand( fldobj, TRUE );

	//OBJ初期化処理の中で描画済みになってしまうため、
	//再度初期描画リクエストをかける（レアカラー反映のため）
	FieldOBJ_SetFirstDrawFlgOFF( fldobj );
	return fldobj;
}

//==============================================================================
//
//	育て屋に預ける前にフォルムを戻す(EvCmdUnionPokeFormReturnの単品バージョン)
//
//==============================================================================
BOOL EvCmdSodateyaPokeFormReturn( VM_MACHINE * core )
{
	POKEMON_PARAM* poke;
	POKEPARTY *party;
	int monsno, form_no;
	u32 set_item;
	int item;
	int ret;
	FIELDSYS_WORK* fsys	= core->fsys;
	u16 poke_pos		= VMGetWorkValue( core );				//手持ち位置
	u16* ret_wk			= VMGetWork( core );

	party	= SaveData_GetTemotiPokemon(fsys->savedata);
	poke	= PokeParty_GetMemberPointer(party, poke_pos);
	*ret_wk = 0;

	//リストをキャンセルした値が入っている時は何もしない
	if( poke_pos == 0xff ){
		return 0;
	}

	//ハッキンダマ確認
	item = PokeParaGet(poke, ID_PARA_item, NULL);

	if(item == ITEM_HAKKINDAMA){

		ret = MyItem_AddItem(	SaveData_GetMyItem(fsys->savedata),
								ITEM_HAKKINDAMA, 1, HEAPID_WORLD);

		if(ret == FALSE){
			*ret_wk = 0xff;	//ハッキンダマを手持ちに戻せなかった
			return 0;
		}

		//バッグに戻したのでハッキンダマを所持しているポケモンのアイテムを無しにする
		set_item = 0;
		PokeParaPut(poke, ID_PARA_item, &set_item);
	}

	//ギラティナ、シェイミ、ロトムのフォルムを戻す
	form_no = PokeParaGet(poke, ID_PARA_form_no, NULL);

	if(form_no > 0){

		monsno = PokeParaGet(poke, ID_PARA_monsno, NULL);

		switch(monsno){
		case MONSNO_KIMAIRAN:	//ギラティナ
			//上でハッキンダマは取り上げているので再計算だけする
			PokeParaGirathinaFormChange(poke);
			break;
		case MONSNO_PURAZUMA:	//ロトム
			PokeParaRotomuFormChange(poke, FORMNO_ROTOMU_NORMAL, 0);
			break;
		case MONSNO_EURISU:		//シェイミ
			PokeParaSheimiFormChange(poke, FORMNO_SHEIMI_NORMAL);
			break;
		}
	}

	return 0;
}

//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------